package com.tencent.qf.team.controller;

import com.alibaba.fastjson.JSON;
import com.tencent.qf.common.pojo.HttpResult;
import com.tencent.qf.mybatis.pojo.PageRequest;
import com.tencent.qf.mybatis.pojo.PageResult;
import com.tencent.qf.team.dal.DmAppSalesOrganizationalPerformance;
import com.tencent.qf.team.dal.DmAppSalesTutorPerformance;
import com.tencent.qf.team.dto.request.PerformanceSummaryOrganizationalHeaderDataRequest;
import com.tencent.qf.team.dto.request.PerformanceSummaryOrganizationalListRequest;
import com.tencent.qf.team.dto.request.TutorPerformanceByGroupIdRequest;
import com.tencent.qf.team.dto.request.TutorPerformanceRequest;
import com.tencent.qf.team.dto.response.PerformanceSummaryPersonalListResponse;
import com.tencent.qf.team.service.PerformanceSummaryService;
import com.tencent.qf.team.service.SalesPerformanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@Api(value = "组织业绩", tags = "组织业绩")
public class SalesPerformanceTutorController {

    @Autowired
    private SalesPerformanceService salesPerformanceService;

    @Autowired
    private PerformanceSummaryService performanceSummaryService;

    @PostMapping("/api/team/performanceSummary/organizationalHeaderData")
    @ApiOperation("组织业绩-头部展示数据")
    public HttpResult<List<DmAppSalesOrganizationalPerformance>> organizationalHeaderData(
            @RequestBody PerformanceSummaryOrganizationalHeaderDataRequest request) {
        log.info("组织业绩-头部展示数据，入参：{}", JSON.toJSONString(request));
        List<DmAppSalesOrganizationalPerformance> data = performanceSummaryService.organizationalHeaderData(request);
        HttpResult<List<DmAppSalesOrganizationalPerformance>> result = HttpResult.success(data);
        log.info("组织业绩-头部展示数据，出参：{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/api/team/performanceSummary/organizationalList")
    @ApiOperation("组织业绩-列表")
    public HttpResult<List<DmAppSalesOrganizationalPerformance>> organizationalList(
            @RequestBody PerformanceSummaryOrganizationalListRequest request) {
        log.info("组织业绩-列表，入参：{}", JSON.toJSONString(request));
        List<DmAppSalesOrganizationalPerformance> data = performanceSummaryService.organizationalList(request);
        HttpResult<List<DmAppSalesOrganizationalPerformance>> result = HttpResult.success(data);
        log.info("组织业绩-列表，出参：{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/api/team/tutor/performance")
    @ApiOperation("组织业绩(导师列表->详情)")
    public HttpResult<DmAppSalesTutorPerformance> tutorPerformance(@RequestBody TutorPerformanceRequest request) {
        log.info("组织业绩(导师列表->详情)，入参：{}", JSON.toJSONString(request));
        DmAppSalesTutorPerformance performance = salesPerformanceService.tutorPerformance(request);
        HttpResult<DmAppSalesTutorPerformance> result = HttpResult.success(performance);
        log.info("组织业绩(导师列表->详情)，出参：{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/api/team/tutor/performance/byGroupId")
    @ApiOperation("组织业绩-查组织下所有导师业绩")
    public HttpResult<List<DmAppSalesTutorPerformance>> tutorPerformanceByGroupId(@RequestBody TutorPerformanceByGroupIdRequest request) {
        log.info("组织业绩-查组织下所有导师业绩，入参：{}", JSON.toJSONString(request));
        List<DmAppSalesTutorPerformance> performance = salesPerformanceService.tutorPerformanceByGroupId(request);
        HttpResult<List<DmAppSalesTutorPerformance>> result = HttpResult.success(performance);
        log.info("组织业绩-查组织下所有导师业绩，出参：{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/api/team/tutor/performance/byGroupId/paginate")
    @ApiOperation("组织业绩-查组织下所有导师业绩-分页")
    public HttpResult<PageResult<DmAppSalesTutorPerformance>> tutorPerformanceByGroupIdPaginate(@RequestBody PageRequest<TutorPerformanceByGroupIdRequest> request) {
        log.info("组织业绩-查组织下所有导师业绩-分页，入参：{}", JSON.toJSONString(request));

        List<String> orderFields = Arrays.asList(new String[]{"sales_order_num", "sales_amt", "sales_atv", "sales_user_num",
                "sales_income_amt", "sales_self_income_amt", "sales_promote_income_amt",
                "sales_income_amt_all", "overcharge_sales_income_amt", "promote_income_amt_all", "self_income_amt_all", "base_income_amt"});
        List<String> orderSorts = Arrays.asList(new String[]{"desc", "asc"});

        HttpResult<PageResult<DmAppSalesTutorPerformance>> result;
        if (!orderFields.contains(request.getData().getOrderField())) {
            result = HttpResult.error(500, "排序字段格式错误");
        } else if (!orderSorts.contains(request.getData().getOrderDirection())) {
            result = HttpResult.error(500, "排序方式格式错误");
        } else {
            PageResult<DmAppSalesTutorPerformance> performance = salesPerformanceService.tutorPerformanceByGroupIdPaginate(request);
            result = HttpResult.success(performance);
        }
        log.info("组织业绩-查组织下所有导师业绩-分页，出参：{}", JSON.toJSONString(result));
        return result;
    }
}
